package moniMaipiao;

public class ThreadExtend2 {
    public static void main(String[] args) {

        ThreadExtendImpl2 t1 = new ThreadExtendImpl2();
        ThreadExtendImpl2 t2 = new ThreadExtendImpl2();
        ThreadExtendImpl2 t3 = new ThreadExtendImpl2();
        t1.setName("名称A");
        t2.setName("名称B");
        t3.setName("名称C");
        t1.start();
        t2.start();
        t3.start();
    }
}
class ThreadExtendImpl2 extends Thread{
//    private static Object o = new Object();  // 不建议new一个新对象， 可以使用字节码文件当做锁。
    private static int ticket = 2000;

    @Override
    public void run() {
        while (true){
            synchronized (ThreadExtendImpl2.class){ // 可以使用字节码文件当做锁。
                if(ticket <=0)break;
                System.out.println(getName()+"第" + ticket+ "张票");
                ticket--;
            }
        }
    }
}